#include "form.h"
#include "ui_form.h"
#include<QMap>
#include<QDebug>
#include <QHostAddress>
#include<QString>
#include <QTcpSocket>


int pictureNum=1;
int balance=1000;
Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{

    ui->setupUi(this);
    m_pSocket=NULL;
    m_pSocket=new QTcpSocket(this);
    m_pSocket->connectToHost(QHostAddress("127.0.0.1"),8888);
    if(m_pSocket->waitForConnected(1000)){
        qDebug()<<"connect ok form";
    }

    pix.load(":/new/prefix1/"+QString::number(pictureNum)+".jpeg");
    ui->label->setPixmap(pix);
    ui->label->setScaledContents(true);
    mapPirce.insert(pictureNum,199);
    pictureNum+=1;

    pix.load(":/new/prefix1/"+QString::number(pictureNum)+".jpeg");
    ui->label_2->setPixmap(pix);
    ui->label_2->setScaledContents(true);
    mapPirce.insert(pictureNum,99);
    pictureNum+=1;

    pix.load(":/new/prefix1/"+QString::number(pictureNum)+".jpeg");
    ui->label_3->setPixmap(pix);
    ui->label_3->setScaledContents(true);
    mapPirce.insert(pictureNum,79);
    pictureNum+=1;

    pix.load(":/new/prefix1/"+QString::number(pictureNum)+".jpeg");
    ui->label_4->setPixmap(pix);
    ui->label_4->setScaledContents(true);
    mapPirce.insert(pictureNum,59);
    pictureNum+=1;

    pix.load(":/new/prefix1/"+QString::number(pictureNum)+".jpeg");
    ui->label_5->setPixmap(pix);
    ui->label_5->setScaledContents(true);
    mapPirce.insert(pictureNum,99);
    pictureNum+=1;

    pix.load(":/new/prefix1/"+QString::number(pictureNum)+".jpeg");
    ui->label_6->setPixmap(pix);
    ui->label_6->setScaledContents(true);
    mapPirce.insert(pictureNum,39);
    pictureNum+=1;

    pix.load(":/new/prefix1/"+QString::number(pictureNum)+".jpeg");
    ui->label_7->setPixmap(pix);
    ui->label_7->setScaledContents(true);
    mapPirce.insert(pictureNum,59);
    pictureNum+=1;

    pix.load(":/new/prefix1/"+QString::number(pictureNum)+".jpeg");
    ui->label_8->setPixmap(pix);
    ui->label_8->setScaledContents(true);
    mapPirce.insert(pictureNum,29);
    pictureNum+=1;
    pix.load(":/new/prefix1/"+QString::number(pictureNum)+".jpeg");
    mapPirce.insert(pictureNum,29);
    if(pictureNum==9) pictureNum=1;
    ui->label_9->setPixmap(pix);
    ui->label_9->setScaledContents(true);
    mapPirce.insert(pictureNum,29);
    mapName.insert(1,"初音");
    mapName.insert(2,"高达初音");
    mapName.insert(3,"从零开始的异界生活");
    mapName.insert(4,"手办玩偶");
    mapName.insert(5,"抱枕玩偶礼物");
    mapName.insert(6,"初音歌谣夏日祭");
    mapName.insert(7,"豌豆荚抱枕");
    mapName.insert(8,"可爱小熊玩偶");
    mapName.insert(9,"可爱猫咪玩偶");


}

Form::~Form()
{

    delete ui;

}








void Form::on_sum_clicked()
{
   //结账
   int num[9];
   num[0]=ui->spinBox_1->value();
   num[1]=ui->spinBox_2->value();
   num[2]=ui->spinBox_3->value();
   num[3]=ui->spinBox_4->value();
   num[4]=ui->spinBox_5->value();
   num[5]=ui->spinBox_6->value();
   num[6]=ui->spinBox_7->value();
   num[7]=ui->spinBox_8->value();
   num[8]=ui->spinBox_9->value();
   int sum[9];
   int sumPrice=0;
    for(int i=0;i<9;i++){
        sum[i]=mapPirce.value(i+1)*num[i];
        sumPrice+=sum[i];
    }
   QString str=" ";
   for(int i=0;i<9;i++){
       if(num[i]!=0){
           str.append(mapName.value(i+1));
           str.append("共计:"+QString::number(sum[i])+"\n ");
       }
   }
   str.append("\n总价="+QString::number(sumPrice));
   qDebug()<<str;
   qDebug()<<QString::number(sumPrice);
   m_pSocket->write(str.toStdString().data());
   if(sumPrice<balance){
       ui->label_11->setText("支付成功");
       balance=balance-sumPrice;
   }else{
       ui->label_11->setText("支付失败");
   }
}

void Form::on_exit2_clicked()
{
    this->hide();
    Widget *w=new Widget();
    w->show();
}



void Form::on_pushButton_clicked()
{
    QString str=ui->lineEdit->text();
    int num=str.toInt();
    balance=balance-num;
    m_pSocket->write(str.toStdString().data());
}


